package com.guiji.text2video.fastvideo.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.guiji.text2video.fastvideo.domain.vo.Text2videoJob;
import com.guiji.text2video.fastvideo.domain.vo.Text2videoPoolsStat;
import com.guiji.text2video.fastvideo.mapper.Text2videoPoolsStatMapper;
import com.guiji.text2video.fastvideo.service.IText2videoJobService;
import com.guiji.text2video.fastvideo.service.IText2videoPoolsStatService;
import com.guiji.text2video.fastvideo.service.IText2videoZipJobService;
import com.guiji.text2video.fastvideo.utils.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Copyright (C), 2018-2021
 * @FileName: CommitJobsTimingTask
 * @Author: scl
 * @Date: 2021/8/30 15:49
 * @Description: 提交任务定时
 */

@Component("commitJobsTimingTask")
public class CommitJobsTimingTask {

    private static final Logger logger = LoggerFactory.getLogger(CommitJobsTimingTask.class);

    @Autowired
    IText2videoPoolsStatService iText2videoPoolsStatService;

    @Autowired(required = false)
    Text2videoPoolsStatMapper pools;

    @Autowired
    IText2videoJobService text2videoJobService;

    @Autowired
    IText2videoZipJobService iText2videoZipJobService;

    /**
     * 每分钟获取合成服务队列，如果为空则提交任务。
     */
    @Scheduled(cron = "5/60 * * * * *")
    public void everyMinCommitJobs() {
        List<Text2videoPoolsStat> poolsStatList = pools.queryLastPoolsCompose();
        if(ObjectUtils.isEmpty(poolsStatList)){
            List<Text2videoJob> jobList = text2videoJobService.selectUnCommitTask();
            if(ObjectUtils.isEmpty(jobList)){
                return;
            }
            logger.info("每分钟提交《四大名著》任务数：{}",jobList.size());
            for (Text2videoJob job : jobList) {
                iText2videoZipJobService.muiltyCommit(job);
            }
        }
    }

}